package mop.gui.project;

import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.table.TableCellRenderer;
import mop.controller.CreateProjectController;
import mop.controller.RemoveProjectController;
import mop.gui.GUI;
import mop.gui.component.JTableButtonModel;
import mop.gui.component.JTableButtonMouseListener;
import mop.gui.component.JTableButtonRenderer;
import mop.model.project.ProjectInterface;

/**
 * List of projects.
 *
 * @author  MOP Team 7
 */
public class ProjectPanel extends javax.swing.JPanel
{
    /**
     * Default constructor.
     */
    public ProjectPanel()
    {
        initComponents();
    }

    /**
     * Constructor.
     *
     * @param   gui
     */
    public ProjectPanel(GUI gui)
    {
        initComponents();
        this.setGui(gui);
    }

    /***************************************************************************
     *                          GUI reference
     ***************************************************************************/

    private GUI gui;

    /**
     * Sets the GUI.
     *
     * @param   gui
     * @post    gui is set.
     */
    private void setGui(GUI gui)
    {
        this.gui = gui;
    }

    /**
     * Returns the GUI reference.
     *
     * @return  this.gui
     */
    public GUI getGui()
    {
        return this.gui;
    }

    /***************************************************************************
     *                             controllers
     ***************************************************************************/

    private CreateProjectController createProjectController;
    private RemoveProjectController removeProjectController;

    /**
     * Adds a controller.
     *
     * @param   createProjectController
     */
    public void addController(CreateProjectController createProjectController) {
        this.createProjectController = createProjectController;
        this.btnCreateProject.setEnabled(true);
    }

    /**
     * Adds a controller.
     *
     * @param   removeProjectController
     */
    public void addController(RemoveProjectController removeProjectController) {
        this.removeProjectController = removeProjectController;
        tableProjects.addMouseListener(new JTableButtonMouseListener(tableProjects));
    }

    /***************************************************************************
     *                             projects table
     ***************************************************************************/

    /**
     * Loads projects in projects table.
     */
    public void update()
    {
        if (this.removeProjectController != null) {
            LinkedList<ProjectInterface> projects = new LinkedList<ProjectInterface>();
            projects = (LinkedList<ProjectInterface>) removeProjectController.getProjectList();

            JTableButtonModel tableModel = new JTableButtonModel();

            String[] columns = {"Description", "Details", "Remove"};
            Object[][] rows = generateProjectRows(projects);

            tableModel.setColumns(columns);
            tableModel.setRows(rows);

            tableProjects.setModel(tableModel);
            TableCellRenderer defaultRenderer = tableProjects.getDefaultRenderer(JButton.class);
            JTableButtonRenderer buttonRenderer = new JTableButtonRenderer(defaultRenderer);
            tableProjects.setDefaultRenderer(JButton.class, buttonRenderer);
        }
    }

    /**
     * Generates the rows of the project table.
     *
     * @param   projects
     *          List of project descriptions.
     * @return  The rows for the table.
     */
    private Object[][] generateProjectRows(LinkedList<ProjectInterface> projects)
    {
        Object[][] rows = new Object[projects.size()][3];

        for ( int i = 0 ; i < projects.size() ; i++ ) {

            rows[i][0] = projects.get(i).getDescription();

            rows[i][1] = new JButton("Details");
            ((JButton)rows[i][1]).addMouseListener(new ProjectDetailsAction(this.getGui(), projects.get(i)));

            rows[i][2] = new JButton("Remove");
            ((JButton)rows[i][2]).addMouseListener(new RemoveProjectAction(this.getGui(), removeProjectController, projects.get(i)));

        }

        return rows;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblProjects = new javax.swing.JLabel();
        projectsPane = new javax.swing.JScrollPane();
        tableProjects = new javax.swing.JTable();
        btnCreateProject = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(517, 569));

        lblProjects.setFont(new java.awt.Font("Tahoma", 1, 18));
        lblProjects.setText("Projects"); // NOI18N

        tableProjects.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        projectsPane.setViewportView(tableProjects);

        btnCreateProject.setText("Create Project"); // NOI18N
        btnCreateProject.setEnabled(false);
        btnCreateProject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateProjectActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(projectsPane, javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lblProjects)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 319, Short.MAX_VALUE)
                        .addComponent(btnCreateProject)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCreateProject)
                    .addComponent(lblProjects))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(projectsPane, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnCreateProjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateProjectActionPerformed
        (new CreateProjectDialog(gui, this.createProjectController)).setVisible(true);
}//GEN-LAST:event_btnCreateProjectActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCreateProject;
    private javax.swing.JLabel lblProjects;
    private javax.swing.JScrollPane projectsPane;
    private javax.swing.JTable tableProjects;
    // End of variables declaration//GEN-END:variables

}
